package fightplan.business;

import java.util.List;

public class SkrKyuFighterUtil {
	public static final String KATA = "Kata";
	public static final String KUMITE = "Kumite";
	public static final String KUMITE_CHALLANGE = "Kumite Challange";

	private static final int firstChallange = 14;
	private static final int lastChallange = 17;

	public static void setCategory(Tournament tournament, AbstractFighter fighter, String categoryName, boolean set) {
		if (set != hasCategory(tournament, fighter, categoryName)) {
			for (Category category : tournament.getCategories()) {
				if (!category.compatible(fighter))
					continue;
				if (KATA.equals(categoryName) && CategoryType.KATA.equals(category.getCategoryType())) {
					set(category, fighter, set);
				}
				if (CategoryType.KUMITE.equals(category.getCategoryType())) {
					if (KUMITE_CHALLANGE.equals(categoryName) && category.getCategoryId() >= firstChallange
							&& category.getCategoryId() <= lastChallange) {
						set(category, fighter, set);
					} else if (KUMITE.equals(categoryName)) {
						set(category, fighter, set);
					}
				}
			}
		}
	}

	private static void set(Category category, AbstractFighter fighter, boolean set) {
		if (set) {
			category.getFighters().add(fighter);
		} else {
			category.getFighters().remove(fighter);
		}
	}

	public static boolean hasCategory(Tournament tournament, AbstractFighter fighter, String categoryName) {
		return hasCategory(tournament, fighter.searchCategories(tournament), categoryName);
	}

	public static boolean hasCategory(Tournament tournament, List<Category> categories, String categoryName) {
		for (int i = 0; i < categories.size(); i++) {
			Category category = categories.get(i);
			if (KATA.equals(categoryName) && CategoryType.KATA.equals(category.getCategoryType())) {
				return true;
			}
			if (CategoryType.KUMITE.equals(category.getCategoryType())) {
				if (KUMITE_CHALLANGE.equals(categoryName) && category.getCategoryId() >= firstChallange
						&& category.getCategoryId() <= lastChallange) {
					return true;
				} else if (KUMITE.equals(categoryName)) {
					return true;
				}
			}
		}
		return false;
	}

}
